package com.happok.onlineclass.usermanagement.feign.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.happok.common.result.CommonResult;
import com.happok.microgateway.service.RoutingDelegate;
import com.happok.onlineclass.usermanagement.common.exception.ForwardException;
import com.happok.onlineclass.usermanagement.feign.dto.RecordAddDTO;
import com.happok.onlineclass.usermanagement.feign.dto.RecordListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/9/24 15:56
 */
@Service
@Slf4j
public class CustomFormServiceImpl implements CustomFormService {

    @Autowired
    private RoutingDelegate routingDelegate;

    @Override
    public String addFormRecord(Integer orgId, String customFormType, Map<String, Object> extendData) throws URISyntaxException {
        RecordAddDTO recordAddDTO = new RecordAddDTO();
        recordAddDTO.setReferenceId(orgId);
        recordAddDTO.setRecordDoc(extendData);
        String url = customFormType + "/records/v1";
        RequestEntity requestEntity = new RequestEntity(recordAddDTO, HttpMethod.POST, new URI(url));
        ResponseEntity<CommonResult> resultResponseEntity = routingDelegate.forward(requestEntity);
        if (!resultResponseEntity.getStatusCode().is2xxSuccessful()) {
            throw new ForwardException(String.valueOf(resultResponseEntity.getStatusCode().value()), resultResponseEntity.getBody().getMsg());
        }
        JSONObject jsonObject = (JSONObject) JSON.toJSON(resultResponseEntity.getBody().getData());
        String recordId = jsonObject.getString("id");
        return recordId;
    }

    @Override
    public ResponseEntity<CommonResult> updateFormRecord(String user, Integer orgId, String recordId, String customFormType, Map<String, Object> extendData) throws URISyntaxException {
        RecordAddDTO recordAddDTO = new RecordAddDTO();
        recordAddDTO.setReferenceId(orgId);
        recordAddDTO.setRecordDoc(extendData);
        String url = customFormType + "/records/" + recordId + "/v1";
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.set("User", user);
        RequestEntity requestEntity = new RequestEntity(recordAddDTO, headers, HttpMethod.PUT, new URI(url));
        ResponseEntity<CommonResult> resultResponseEntity = routingDelegate.forward(requestEntity);
        if (!resultResponseEntity.getStatusCode().is2xxSuccessful()) {
            throw new ForwardException(String.valueOf(resultResponseEntity.getStatusCode().value()), resultResponseEntity.getBody().getMsg());
        }
        return resultResponseEntity;
    }

    @Override
    public Map<String, Object> getFormRecord(String recordId, String customFormType) throws URISyntaxException {
        String url = customFormType + "/records/" + recordId + "/v1";
        RequestEntity requestEntity = new RequestEntity(HttpMethod.GET, new URI(url));
        ResponseEntity<CommonResult> resultResponseEntity = routingDelegate.forward(requestEntity);
        if (!resultResponseEntity.getStatusCode().is2xxSuccessful()) {
            throw new ForwardException(String.valueOf(resultResponseEntity.getStatusCode().value()), resultResponseEntity.getBody().getMsg());
        }
        Map<String, Object> resultData = (Map<String, Object>) resultResponseEntity.getBody().getData();
        return resultData;
    }

    @Override
    public List<Integer> listFormRecord(String customFormType, Map<String, Object> extendData) throws URISyntaxException {
        RecordListQuery recordListQuery = new RecordListQuery();
        recordListQuery.setOnlyReturnReferenceIds(true);
        recordListQuery.setFilterParams(extendData);
        String url = customFormType + "/records/batch/v1";
        RequestEntity requestEntity = new RequestEntity(recordListQuery, HttpMethod.POST, new URI(url));
        ResponseEntity<CommonResult> resultResponseEntity = routingDelegate.forward(requestEntity);
        if (!resultResponseEntity.getStatusCode().is2xxSuccessful()) {
            throw new ForwardException(String.valueOf(resultResponseEntity.getStatusCode().value()), resultResponseEntity.getBody().getMsg());
        }
        JSONObject jsonObject = (JSONObject) JSON.toJSON(resultResponseEntity.getBody().getData());
        List<Integer> referenceIds = jsonObject.getJSONArray("result").toJavaList(Integer.class);
        return referenceIds;
    }
}